package com.funymmz.demo.webapi;


import com.funymmz.demo.domain.User;
import com.funymmz.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/webapi/login")
public class LoginRestController {

    @Autowired
    private UserService userService;

    @GetMapping("/user")
    public int user(HttpServletRequest httpServletRequest, String username, String password){

        //判断账户是否存在
        List<User> users = userService.findByUsernameAndPassword(username, password);

        if (users.size()>0){
            //如果存在，写入session
            HttpSession httpSession =httpServletRequest.getSession();
            httpSession.setAttribute("userid", users.get(0).getId());
            httpSession.setAttribute("user", users.get(0));

            //成功
            return 1;
        }
        else {
            //如果不存在
            return -1;
        }
    }
}
